﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace NSISBg
{
    /// <summary>
    /// Button.xaml 的交互逻辑
    /// </summary>
    public partial class Button : MetroWindow
    {
        public Button()
        {
            InitializeComponent();
        }
        //private void A()
        //{
        //    Window w = Window.GetWindow(canvas_Install);
        //    System.Windows.Point p = canvas_Install.TransformToAncestor(w).Transform(new System.Windows.Point(0, 0));
        //    var p1 = PointToScreen(p);
        //    //MessageBox.Show("(" + p.X + "," + p.Y + "),("+p1.X + "," + p1.Y + ")");
        //    var bitmapSource = CopyScreen((int)p1.X, (int)p1.Y, (int)canvas_Install.ActualHeight, (int)canvas_Install.ActualWidth);
        //    using (var fileStream = new FileStream("image.bmp",FileMode.Create))
        //    {
        //        BitmapEncoder encoder = new BmpBitmapEncoder();
        //        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
        //        encoder.Save(fileStream);
        //    }
        //}
        private System.Windows.Media.Imaging.BitmapSource CopyScreen(int left, int top, int width, int heigh)
        {
            using (var screenBmp = new Bitmap(width, heigh, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                using (var bmpGraphics = Graphics.FromImage(screenBmp))
                {
                    bmpGraphics.CopyFromScreen(left, top, 0, 0, screenBmp.Size);
                    return Imaging.CreateBitmapSourceFromHBitmap(
                        screenBmp.GetHbitmap(),
                        IntPtr.Zero,
                        Int32Rect.Empty,
                        System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                }
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //Window w = Window.GetWindow(btn_install);
            //System.Windows.Point p = btn_install.TransformToAncestor(w).Transform(new System.Windows.Point(0, 0));
            //var p1 = PointToScreen(p);
            //var bitmapSource = CopyScreen((int)p1.X, (int)p1.Y, (int)btn_install.ActualWidth, (int)btn_install.ActualHeight);
            //using (var fileStream = new FileStream("image.bmp", FileMode.Create))
            //{
            //    GenerateImage(bitmapSource, ImageFormat.BMP, fileStream);
            //}
            //System.Diagnostics.Process.Start("image.bmp");


            //SaveControlImage(btn_close);
            SaveControlImage(btn_install);
            //SaveControlImage(btn_check);
        }

        //private RenderTargetBitmap RenderVisaulToBitmap(Visual vsual, int width, int height)
        //{
        //    var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
        //    rtb.Render(vsual);

        //    return rtb;
        //}
        private void SaveControlImage(StackPanel control)
        {
            Window w = Window.GetWindow(control);
            System.Windows.Point p = control.TransformToAncestor(w).Transform(new System.Windows.Point(0, 0));
            var p1 = PointToScreen(p);
            var bitmapSource = CopyScreen((int)p1.X, (int)p1.Y, (int)control.ActualWidth, (int)control.ActualHeight);
            using (var fileStream = new FileStream("image.bmp", FileMode.Create))
            {
                GenerateImage(bitmapSource, ImageFormat.BMP, fileStream);
            }
            System.Diagnostics.Process.Start("image.bmp");
        }
        private void GenerateImage(BitmapSource bitmap, ImageFormat format, Stream destStream)
        {
            BitmapEncoder encoder = null;

            switch (format)
            {
                case ImageFormat.JPG:
                    encoder = new JpegBitmapEncoder();
                    break;
                case ImageFormat.PNG:
                    encoder = new PngBitmapEncoder();
                    break;
                case ImageFormat.BMP:
                    encoder = new BmpBitmapEncoder();
                    break;
                case ImageFormat.GIF:
                    encoder = new GifBitmapEncoder();
                    break;
                case ImageFormat.TIF:
                    encoder = new TiffBitmapEncoder();
                    break;
                default:
                    throw new InvalidOperationException();
            }

            encoder.Frames.Add(BitmapFrame.Create(bitmap));
            encoder.Save(destStream);
        }
        public enum ImageFormat { JPG, BMP, PNG, GIF, TIF }
    }
}
